﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DataAccess
{
    public class EmployeeDataAccess
    {

        public int EmployeeRegister(EmployeeInfo employee)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var emp = db.Employees.Where(e => e.Account.Equals(employee.Account)).SingleOrDefault();
            var empEmail = db.Employees.Where(e => e.Email.Equals(employee.Email)).SingleOrDefault();
            if (emp == null)
            {
                Employee e = new Employee();
                e.Firstname = employee.Firstname;
                e.Lastname = employee.Lastname;

                e.Account = employee.Account;
                e.Password = employee.Password;
                e.Dob = employee.Dob;
                e.Address = employee.Address;
                e.Phone = employee.Phone;
                e.Department = employee.Department;
                if (empEmail == null)
                {
                    e.Email = employee.Email;
                }
                else
                {
                    return 4; //email is duplicate
                }

                try
                {
                    db.Employees.InsertOnSubmit(e);
                    db.SubmitChanges();
                    return 1; // register successfull
                }
                catch (Exception ex)
                {
                    return 2; // register fail
                }

            }
            else
            {
                return 3; // account is exist
            }
        }

        public EmployeeInfo EmployeeLogin(string account, string password)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var emp = db.Employees.Where(c => c.Account.Equals(account) && c.Password.Equals(password)).SingleOrDefault();
            if (emp != null) //found
            {
                EmployeeInfo e = new EmployeeInfo();
                e.EmployeeID = emp.EmployeeID;
                e.Firstname = emp.Firstname;
                e.Lastname = emp.Lastname;
                e.Account = emp.Account;
                e.Password = emp.Password;
                e.Dob = emp.Dob;
                e.Address = emp.Address;
                e.Phone = emp.Phone;
                e.Email = emp.Email;
                e.Department = Convert.ToInt32(emp.Department);

                return e;
            }
            else // not found
            {
                return null;
            }
        }

        public Boolean ConfirmPassword(int empID, string password)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var emp = db.Employees.Where(c => c.EmployeeID.Equals(empID) && c.Password.Equals(password)).SingleOrDefault();
            if (emp != null) //found
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void UpdateInfomation(EmployeeInfo employee)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var emp = db.Employees.Where(c => c.EmployeeID == employee.EmployeeID).SingleOrDefault();
            var empEmail = db.Employees.Where(c => c.Email.Equals(employee.Email)).SingleOrDefault();
            if (emp != null) // found
            {
                emp.Firstname = employee.Firstname;
                emp.Lastname = employee.Lastname;
                emp.Dob = employee.Dob;
                emp.Address = employee.Address;
                emp.Phone = employee.Phone;
                emp.Email = employee.Email;

                db.SubmitChanges();
            }
        }

        public Boolean UpdatePassword(int empID, string newPass)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var emp = db.Employees.Where(c => c.EmployeeID.Equals(empID)).SingleOrDefault();
            if (emp != null)
            {
                emp.Password = newPass;
                db.SubmitChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
